//
//  HZPeiziHomeView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/23.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziHomeView.h"
#import "HZPeiziAccountView.h"
#import "HZPeiziMiddleView.h"

#define kCellH 44

@interface HZPeiziHomeView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIScrollView *pScrollView;
@property (nonatomic, strong)UIView *pContentSizeView;
@property (nonatomic, strong)HZPeiziAccountView *pAccountView;
@property (nonatomic, strong)HZPeiziMiddleView *pMiddleView;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pDataList;

@property (nonatomic, strong)NSArray *pListImage;
@end

@implementation HZPeiziHomeView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon
{
    _pScrollView = [UIScrollView new];
    _pContentSizeView = [UIView new];
    _pAccountView = [HZPeiziAccountView new];
    _pMiddleView = [HZPeiziMiddleView new];
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.bounces = NO;
    
    _pDataList = @[@"追加保证金", @"我的持仓", @"委托查询",@"成交查询", @"资金流水", @"提取盈利",@"更多账户"];
    _pListImage = @[@"zjls",@"grzx_icon_ye",@"grzx_zg",@"grzx_cj",@"bzj",@"grzx_icon_yhk",@"grzx_icon_gr"];
    
    if (@available(iOS 11.0, *)) {
        _pScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}
- (void)initConstraints
{
    [self addSubview:_pScrollView];
    [_pScrollView addSubview:_pContentSizeView];
    [_pContentSizeView addSubview:_pAccountView];
    [_pContentSizeView addSubview:_pMiddleView];
    [_pContentSizeView addSubview:_pTableView];
    
    [_pScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    [_pContentSizeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_pScrollView);
        make.bottom.mas_equalTo(_pTableView.mas_bottom);
        make.width.mas_equalTo(self);
    }];
    [_pAccountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(_pContentSizeView);
        make.height.mas_equalTo(120);
    }];
    [_pMiddleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pAccountView.mas_bottom).offset(10);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(66);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pMiddleView.mas_bottom).offset(10);
        make.left.right.mas_equalTo(_pContentSizeView);
        make.height.mas_equalTo(kCellH*_pDataList.count + 1);
    }];
}

- (void)updateTheme
{
    //    _pBgImageView.image = [UIImage imageNamed:[[ThemeManager manager] getThemeImageWithName:@"me_bg"]];
}

- (void)updateAccontMoneyData:(HZPeiziAccountMoneyModel *)money
{
    [_pAccountView updateAccountData:money];
}
#pragma action

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    NSObject *model = _pDataList[indexPath.row];
    NSString *imageName = _pListImage[indexPath.row];
    static NSString *ID = @"me_normalCell";
    cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.separatorInset = UIEdgeInsetsZero;
    cell.textLabel.textColor = kMYHexToRGB(0x888888, 1);
    cell.imageView.image = [UIImage imageNamed:imageName];
    cell.textLabel.text = (NSString *)model;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellH;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *model = _pDataList[indexPath.row];
    NSString *nextRouter = nil;
    if ([model isEqual:@"追加保证金"]) {
        nextRouter = kRouterPeiziAddMoney;
    }else if ([model isEqual:@"我的持仓"]) {
        nextRouter = kRouterPeiziMYStock;
    }else if ([model isEqual:@"委托查询"]) {
        nextRouter = kRouterPeiziWeiTuo;
    }else if ([model isEqual:@"成交查询"]) {
        nextRouter = kRouterPeiziDeal;
    }else if ([model isEqual:@"资金流水"]) {
        nextRouter = kRouterPeiziMoneyFlow;
    }else if ([model isEqual:@"提取盈利"]) {
        nextRouter = kRouterPeiziMoneyOut;
    }else if ([model isEqual:@"更多账户"]) {
        nextRouter = @"更多账户";
    }
    
    LogicRouterBlock logicRouter = [self.logicManager matchLogicBlock:kLogicRouterPeiziClickList];
    if (logicRouter) {
        logicRouter(nil, nextRouter);
    }
}


@end
